/**
 * 
 */
package tstad;

import java.util.HashMap;

/**
 * @author ito
 * 
 */
public class TSTAD {
	private HashMap<Integer, HashMap<String, Record>> tabela;

	public TSTAD() {
		tabela = new HashMap<Integer, HashMap<String, Record>>();
		createNewNivel();
	}

	public void createNewNivel() {
		tabela.put(tabela.size(), new HashMap<String, Record>());
	}

	public void deleteLastNivel() {
		tabela.remove(tabela.get(getLastNivel()));
	}

	public int getLastNivel() {
		return tabela.size() - 1;
	}

	public boolean addRecord(Record reg, int nivel) {
		HashMap<String, Record> tmp = tabela.get(nivel);
		if (tmp != null) {

			if (tmp.containsKey(reg.getIdent()))
				return false;
			else {
				tmp.put(reg.getIdent(), reg);
				return true;
			}
		} else {
			System.out.println("nao existe o nivel!!!");
			return false;
		}
	}

	public Record searchRecord(Record rec) {
		Record result = null;
		for (int c = getLastNivel(); c >= 0; c--) {
			HashMap<String, Record> tmp = tabela.get(c);
			result = tmp.get(rec.getIdent());

			if (result != null) {
				return result;
			}
		}
		return null;
	}

	public Record searchRecordCurrLvl(Record rec) {
		Record result = null;
		HashMap<String, Record> tmp = tabela.get(getLastNivel());
		result = tmp.get(rec.getIdent());

		if (result != null) {
			return result;
		}
		return result;
	}
}
